/*
CodeBlueCaseNotification_Controller
Version 1.0
Description: Controller class for visualforce page CodeBlueCaseNotification.
Author: Pratibh Prakash
Created Date: 23 Feb-2009
*/
public class CodeBlueCaseNotification_Controller {
    
    private ID caseId;
    private Case objCase, tempCase; 
    private Account account;
    private String additionalTo='', subject;
    private Task[] task;
    private Boolean caseClose = false;//Variable to get status if Case record is going 
    private Map<String, String> parameters = new Map<String, String>();//Map for storing parameters retreived from URL.
    /*
    CodeBlueCaseNotification_Controller(ApexPages.StandardController controller): Contructor 
    Intializes class properties according to different condtions. 
    */
    public CodeBlueCaseNotification_Controller(ApexPages.StandardController controller) {
        
        tempCase = (Case)controller.getRecord();
        if(tempCase.Id!= null){//Checking if record is present in current context or Case record Id has to be retreived from URL.
            caseId = tempCase.Id;
            
        }
        else{//This block executes on 'CaseClose'<Custom Button with Javascript that contructs and redirects URL> button click on Case detail page. 
            PageReference currentPage = ApexPages.currentPage();
            parameters = currentPage.getParameters();
            caseId = parameters.get('caseid');
            caseClose = true;
        }
        try{
            objCase =[select Id, AccountId, Code_Blue_Active__c, Status from Case where id =: caseId ];
            account = [select Name, Code_Blue_Account_Notification_List__c, Code_Blue_Regional_Notification_List__c from Account where id =: objCase.AccountId];
            task = [select Description from Task where WhatId =: objCase.Id and Type__c =: 'Code Blue Notification' ORDER BY LastModifiedDate DESC];
        
            if(objCase.Code_Blue_Active__c == false){//Condition for creation of task with subject as 'Code Blue Notification – New - <Account Name>'
                additionalTo = account.Code_Blue_Account_Notification_List__c+'\n'+account.Code_Blue_Regional_Notification_List__c;
                subject = 'Code Blue Notification – New -'+account.Name;
            }
            else{
                if(task.size() ==0){//Checking if there is any task related to Case record in current context.
                    additionalTo = '';
                    if( ((objCase.Status).equals('Closed')) == false){//Condition for creation of task with subject as 'Code Blue Notification – Update - <Account Name>'
                        subject = 'Code Blue Notification – Update -'+account.Name;
                    }
                    if( ((objCase.Status).equals('Closed')) == true){//Condition for creation of task with subject as 'Code Blue Notification – Closed - <Account Name>'
                        subject = 'Code Blue Notification – Closed -'+account.Name;
                    }
                    if(caseClose == true){
                        subject = 'Code Blue Notification – Closed -'+account.Name;
                    }
                }
                else{
                    getRecentTask();
                    if( ((objCase.Status).equals('Closed')) == false){//Condition for creation of task with subject as 'Code Blue Notification – Update - <Account Name>'
                        subject = 'Code Blue Notification – Update -'+account.Name;
                    }
                    if( ((objCase.Status).equals('Closed')) == true){//Condition for creation of task with subject as 'Code Blue Notification – Closed - <Account Name>'
                        subject = 'Code Blue Notification – Closed -'+account.Name;
                    }
                    if(caseClose == true){
                        subject = 'Code Blue Notification – Closed -'+account.Name;
                    }
                }
            }
        }
        catch(Exception e){
            ApexPages.addMessages(e);
        }
    }
    
    /*
    PageReference pageForward()
    Description: Method redirects control to Send Email page and populates 'Related To', 'Additional To' and 'Subject' fields on the page.
    */
    public PageReference pageForward(){
        
        if(tempCase.Id!= null){
            PageReference emailPage = new PageReference('/_ui/core/email/author/EmailAuthor?p3_lkid='
                                                        +objCase.Id+'&retURL=%2F'+objCase.Id+'&p24='
                                                        +additionalTo+'&p6='+subject+'&p5=');
            if(objCase.Code_Blue_Active__c == false){//Validating if Code_Blue_Active = false then setting it to true.
                try{
                    objCase.Code_Blue_Active__c = true;
                    update(objCase);
                }
                catch(DMLException e){
                    ApexPages.addMessages(e);
                }
            }
            emailPage.setRedirect(true);
            return emailPage;
        }
        else{
            PageReference emailPage = new PageReference('/_ui/core/email/author/EmailAuthor?p3_lkid='
                                                        +objCase.Id+'&retURL=%2F'+objCase.Id+'&saveURL=/'
                                                        +objCase.Id+'/s?retURL=/'+objCase.Id+'&cancelURL=%2F/'+objCase.Id+'&p24='+additionalTo+'&p5='
                                                        +'&p6='+subject);
        
            emailPage.setRedirect(true);
            return emailPage;
        }
    }
    
    /*
    getRecentTask()
    Description: Method retreives values from "Description' field of most recent Task record related to Case record in current context.
    */
    void getRecentTask(){
        
        String additional = (task[0].Description).substring(0 , (task[0].Description).indexof('CC'));
        additional = additional.substring(('Additional To: ').length() , additional.length());
        
        String [] values =  additional.split(';',-1);
        
        for(Integer i=0; i<values.size();i++){
            additionalTo += values[i]+'\n';
        }
    }
}